package controllers;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.imageio.ImageIO;
import model.SearchPhotoModel;
import view.ViewPhotoSampleGUI;
import client.CliMessage;
import client.IObserve;
import client.ImageSerializer;

/**
 * The Class ViewPhotoSampleController.
 */
public class ViewPhotoSampleController extends AbstractController implements IObserve {

	/** The view photo sample view. */
	private ViewPhotoSampleGUI viewPhotoSampleView;
	
	/** The search photo model. */
	private SearchPhotoModel searchPhotoModel;
	
	 /** The photos. */
	 private ArrayList<SearchPhotoModel> photos;
	   
	/** The hundle sample. */
	private boolean hundleSample=false;
	
	/**
	 * Instantiates a new view photo sample controller.
	 *
	 * @param theModel the the model
	 */
	public ViewPhotoSampleController(SearchPhotoModel theModel){
		super(null,theModel);
		searchPhotoModel = theModel;	
	}
	
	/**
	 * Instantiates a new view photo sample controller.
	 * @param theView the the view
	 * @param theModel the the model
	 */
	public ViewPhotoSampleController(ViewPhotoSampleGUI theView,SearchPhotoModel theModel){
		super(theView,theModel);
		viewPhotoSampleView = theView;
		searchPhotoModel = theModel;
		searchDB("getphoto" + "SELECT * "
							  +"FROM photos AS p "
							  +"WHERE p.TimeStamp = '" + searchPhotoModel.getTimeStamp() + "' "
							  +"AND p.Location = "+searchPhotoModel.getLocation());
	}
	
	/**
	 * Search db.
	 *
	 * @param sql the sql
	 */
	private void searchDB(String sql){
		sendToServer(sql);
	}
	
	

	/**
	 * This method resizes the image to the given size.
	 * @param img the img
	 * @return the buffered image
	 */
	private BufferedImage resizedImage(BufferedImage img){
		BufferedImage newImage = new BufferedImage(viewPhotoSampleView.getSamplePanelWidth(), viewPhotoSampleView.getSamplePanelHeight(), BufferedImage.TYPE_INT_RGB);
		Graphics g = newImage.createGraphics();
		g.drawImage(img, 0, 0,viewPhotoSampleView.getSamplePanelWidth(), viewPhotoSampleView.getSamplePanelHeight(), null);
		g.dispose();
		return newImage;
	}
	
	/**
	 * This class handles incoming photo from the Data Base.
	 * It gets is as a string and converts it into a BufferedImage and displays it.
	 * @param msgWithSerializedImage - "answer" from the Data Base.
	 */
	@Override
	public void handlePhoto(CliMessage msgWithSerializedImage){
		BufferedImage image;
		String serializedPhoto ;
		ImageSerializer imageSerializer = new ImageSerializer(""); // null string - we don't save photos on hard drive
		byte[] output ;
		ByteArrayInputStream bais;
		ArrayList<HashMap<String,String>> resultToDisplay = msgWithSerializedImage.getSrs();
		try {
			
			serializedPhoto = resultToDisplay.get(0).get("PHOTO");
			output = imageSerializer.decodeImage(serializedPhoto);
			bais = new ByteArrayInputStream(output);
			image = ImageIO.read(bais);
			
			//sample success 
			hundleSample=true;
			viewPhotoSampleView.displayImage(resizedImage(image));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

    /**
     * Sets the photos.
     *
     * @param pics the pics
     */
    public void SetPhotos(ArrayList<SearchPhotoModel> pics)
    {
    	photos=new ArrayList<SearchPhotoModel>();
    	
    	for(int i=0;i<pics.size();i++)
    	{
    		photos.add(new SearchPhotoModel(pics.get(i).getTimeStamp(),pics.get(i).getLocation(),pics.get(i).getLinkToFile()));
    	}
    }
    
    /**
     * Check sample.
     *
     * @return true, if successful
     */
    public boolean checkSample()
    {
    	for(int i=0;i<photos.size();i++)
    	{
    		if (photos.get(i).getLocation()==searchPhotoModel.getLocation() && photos.get(i).getTimeStamp().equals(searchPhotoModel.getTimeStamp()))
    				return true;
    		
    	}
    	
    	return false;
    }
    
	/**
	 * Handle sample success.
	 *
	 * @return true, if successful
	 */
	public boolean handleSampleSuccess()
	{
		if (checkSample())
		{
			hundleSample=true;
			System.out.println("View Sample");
		return hundleSample;
		}
		
		System.out.println("Problem With Sample");
		return hundleSample;
	}


	@Override
	public void handleDBRequestSuccess() {
		
		
	}

	
	@Override
	public void handleNullSearchResult() {
		hundleSample=false;
		
	}


	@Override
	public void handleDBSearchResult(CliMessage dbResult) {
		// TODO Auto-generated method stub
		
	}
}
